﻿using System;
using System.Collections.Generic;

using System.Text;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Base class for controls that can be toggles between states, such as check boxes and radio buttons.
	/// </summary>
	public abstract class BaseToggleControl : BaseFieldControl<bool>
	{
		#region PUBLIC
		/// <summary>
		/// Returns true.
		/// </summary>
		public override bool CanReceiveFocus
		{
			get
			{
				return true;
			}
		}
		#endregion
		#region INTERNAL
		#region PROPS
		/// <summary>
		/// Gets or sets "on" if control is toggled on and "" otherwise.
		/// </summary>
		protected internal override string SerializedValue
		{
			get
			{
				return this.Value ? "on" : "";
			}
			set
			{
				if (string.Equals(value, "on"))
					this.Value = true;
				else
					this.Value = false;
			}
		}
		#endregion
		#region METHODS
		#region INIT
		/// <summary>
		/// Registers InputControl.js and ToggleControl.js.
		/// </summary>
		protected internal override void RegisterIncludes(IncludeCollection includes)
		{
			base.RegisterIncludes(includes);

			includes.Add(new ScriptResourceInclude("TA.Turbo.Res.JS.Simple.InputControl.js", typeof(BaseCommandControl)));
			includes.Add(new ScriptResourceInclude("TA.Turbo.Res.JS.Simple.ToggleControl.js", typeof(BaseCommandControl)));
		}
		#endregion
		#region RENDER
		/// <summary>
		/// Adds the checked attribute.
		/// </summary>
		/// <param name="writer"></param>
		protected override void AddValueAttributes(System.Web.UI.HtmlTextWriter writer)
		{
			if (this.Value)
				writer.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Checked, "checked");
		}
		#endregion
		#endregion
		#endregion
	}
}
